// pages/home/productDetail/productDetail.js
const app = getApp()
const request = require("../../../utils/network.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgBase: app.imgBase,
    product: {},
    region:[],
    customItem: '全部',
    isOpenTypeModal: false,
    actionPullTypeModal: false,
    chooseType: {},
    seletedName: "",
    buyNum: 1,
    buyType: "",
    ifCollect: false,
    freightPrice:0.00
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //获取商品详情
    var url = '/goods/getGoodsInfo';
    request.request(url, { id: options.id }, function (res) {
      that.setData({
        product: res.data,
        ifCollect: res.data.collect
      })

      //获取收货地址
      url = '/userAddress/getUserAddress';
      request.request(url, null, function (res) {
        if (res.data.length > 0) {
          var address = res.data[0];
          that.setData({
            region: [address.provinceName, address.cityName, address.districtName]
          })
        } else {
          that.setData({
            region: ['浙江省']
          })
        }
        that.setFreightPrice()
      }, function () {

      }, 'GET')
    }, function () {

    }, 'GET')
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /* 点击加入购物车 */
  addCart() {
    this.openTypeChooseModal();
  },
  /* 点击立即购买 */
  quickBuy() {
    this.openTypeChooseModal();
    this.setData({
      buyType: "quick",
    });
  },
  /* 选择不同的类型 */
  typeChooseChange(event) {
    var index = event.detail.value;
    this.setData({
      chooseType: this.data.product.styleList[index],
      seletedName: this.data.product.styleList[index].name
    });
  },
  /* 打开类型选择框 */
  openTypeChooseModal() {
    this.setData({
      actionPullTypeModal: true,
      isOpenTypeModal: true
    });
    if (this.data.product.styleList && this.data.product.styleList.length > 0) {
      if (this.data.chooseType.id == null) {
        this.setData({
          buyType: "cart",
          chooseType: this.data.product.styleList[0]
        });
      }else{
      }
    }
  },
  /* 关闭类型选择框 */
  cancelTypeChooseModal() {
    var that = this;
    that.setData({
      actionPullTypeModal: false
    })
    setTimeout(function () {
      that.setData({
        isOpenTypeModal: false
      })
    }, 450);
  },
  /* 提交选择的类型 */
  submit() {
    if (this.data.seletedName != "") {
      var url = "";
      this.cancelTypeChooseModal();
      /* 立即购买 */
      if (this.data.buyType == "quick") {
        url = '../productOrder/productOrder?id=' + this.data.product.id + '&typeId='
          + this.data.chooseType.id + '&number=' + this.data.buyNum;
        wx.navigateTo({
          url: url
        })
      }
      /* 加入购物车 */
      else {
        this.addToCart();
      }
      this.setData({
        buyType: ""
      })
    } else {
      wx.showToast({
        title: "你还没选择商品类型",
        icon: "none"
      })
    }
  },
  addToCart(){
    var goodsCart = {
      createTime: null,
      goodsId: this.data.product.id,
      goodsName: this.data.product.name,
      goodsStyleId: this.data.chooseType.id,
      goodsStyleName: this.data.chooseType.name,
      id: null,
      status: null,
      styleNum: this.data.buyNum,
      userId: 0,
      userName: null
    };
    var url = '/goodsCart/addGoodsToCart';
    request.request(url, goodsCart, function (res) {
      wx.showToast({
        title: res.msg
      });
      if (res.status == 200) {
        wx.navigateTo({
          url: '../productCart/productCart'
        })
      }
    }, function () {

    }, 'POST');
  },
  minuNum() {
    if (this.data.buyNum > 1) {
      this.setData({
        buyNum: Number(this.data.buyNum) - 1
      })
    }
  },
  plusNum() {
    if (this.data.buyNum < this.data.chooseType.balance) {
      this.setData({
        buyNum: Number(this.data.buyNum) + 1
      })
    }
  },
  /* 加入收藏 */
  addCollect() {
    var that = this;
    var id = this.data.product.id;
    var url = '/goodsCollect/handleGoodsToCollect';
    request.request(url, { goodsId: id }, function (res) {
      wx.showToast({
        title: res.msg
      })
      that.setData({
        ifCollect: !that.data.ifCollect
      });
    }, function () {

    }, 'GET');
  },
  /* 修改收货地址 */
  changeAddress(e) {
    this.setData({
      region: e.detail.value
    })
    this.setFreightPrice();
  },
  /* 获取邮费 */
  setFreightPrice() {
    var that = this;
    var url = '/shopFreight/getFreightPrice';
    request.request(url, { provinceName: that.data.region[0] }, function (res) {
      if (res.data == null){
        wx.showToast({
          title: '请重新选择省份',
          icon:'none'
        })
      }else{
        var firstPrice = res.data.firstPrice
        var addPrice = res.data.addPrice
        var weight = that.data.product.styleList[0].weight
        if (weight <= 1) {
          that.setData({
            freightPrice: firstPrice
          });
        } else {
          that.setData({
            freightPrice: firstPrice + (weight - 1) * addPrice
          });
        }
      }
      
    }, function () {

    }, 'GET');
  }
})